<template>
  <div class="square-block home-section">
    <div class="home-title">模型广场</div>
    <div class="square-content">
      <div
          v-for="(item, index) in modelList"
          class="item"
          :class="{ 'is-active': index === activeIndex }"
          :style="itemStyle(index)"
          :key="item.id"
          @click="changeActive(index)"
      >
        <div class="cover"></div>
        <div class="inner">
          <div class="inner-title">{{ item.elementTitle || "title" }}</div>
          <div class="inner-sub-title" v-show="isActive(index)">
            {{ item.elementFrame }}
          </div>
          <div class="inner-content">
            <div v-html="item.remark"></div>
          </div>
          <div class="inner-tags" v-show="isActive(index)">
            <span
                v-for="(tag, _index) in item.elementTagList?.slice(0, 4) || []"
                :key="_index"
                class="tag"
            >{{ tag }}</span
            >
          </div>
          <img
              src="@/assets/more-arrow.svg"
              v-show="!isActive(index)"
              alt=""
              class="more-icon"
          />
          <router-link
              :to="`/model/square/detail?id=${item.id}`"
              target="_blank"
          >
            <div class="active-more" v-show="isActive(index)">
              <span>查看</span>
              <img src="@/assets/more-arrow-white.svg" alt="" />
            </div>
          </router-link>
        </div>
      </div>
    </div>
    <More path="/model/square" />
  </div>
</template>

<script setup>
import More from "./more.vue";
import { ref, computed, onMounted, inject } from 'vue';

const modelList = ref([]);
const activeIndex = ref(0);
const changeActive = (index) => {
  activeIndex.value = index;
};

const itemStyle = computed(() => {
  return (index) => {
    return {
      backgroundImage: `url('/square-item-${index + 1}.png')`,
    };
  };
});
const isActive = computed(() => {
  return (index) => {
    return index === activeIndex.value;
  };
});

const $api = inject("$api");
const getData = async () => {
  const {data} = await $api.provide.getCustomerList({
    pageNum: 1,
    pageSize: 4,
    elementShareType: 1,
    elementType: 2,
    status: 1,
  });
  modelList.value = data;
};

onMounted(() => {
  getData();
});
</script>

<style lang="less" scoped>
.square-block {
  padding: 80px 0 72px;
  background: #fff;

  .square-content {
    width: 95%;
    margin: 48px auto 40px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .item {
    position: relative;
    box-sizing: border-box;
    height: 400px;
    width: calc(20% - 12px); /* 5 items per row with gap */
    margin-bottom: 16px;
    transition: all 0.4s;
    overflow: hidden;
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;

    .cover,
    .inner {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
    }

    .cover {
      opacity: 0;
      background: linear-gradient(180deg,
      rgba(7, 73, 242, 0.8) 0%,
      rgba(7, 73, 242, 0.8) 100%);
      transition: opacity 0.4s ease-in-out;
    }

    .inner {
      box-sizing: border-box;
      padding: 56px 34px 37px 29px;

      &-title,
      &-sub-title {
        font-weight: 500;
        font-size: 24px;
        color: @text-default;
        margin-bottom: 13px;
      }

      &-content {
        font-size: 14px;
        line-height: 20px;
        color: @text-light;
        .multiline-overflow(8);
      }

      .more-icon {
        position: absolute;
        width: 24px;
        height: 24px;
        bottom: 37px;
      }

      .active-more {
        position: absolute;
        right: 34px;
        bottom: 31px;
        background: #ff0d73;
        width: 90px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;

        span {
          font-size: 18px;
          color: #fff;
          margin-right: 3px;
        }

        img {
          width: 18px;
          height: 18px;
        }
      }
    }

    .tag {
      color: #fff;
      font-size: 14px;
      line-height: 20px;
      padding: 3px 19px;
      border-radius: 2px;
      background: rgba(255, 255, 255, 0.3);
      margin-right: 13px;
    }

    &:first-child {
      margin-left: 0;
    }

    &.is-active {
      width: calc(40% - 12px); /* 2 items per row with gap */
      height: auto; /* Auto height to fit content */

      .cover {
        opacity: 1;
      }

      .inner {
        padding-bottom: 0;
        padding-right: 160px;

        > div {
          color: #fff;
        }

        &-title {
          margin-bottom: 22px;
        }

        &-sub-title {
          margin-bottom: 16px;
        }

        &-content {
          margin-bottom: 40px;
          .multiline-overflow(3);
        }
      }
    }
  }
}

/* Mobile styles */
@media screen and (max-width: 768px) {
  .square-content {
    display: block;
    width: 100%;
    padding: 0 16px;
  }

  .item {
    width: calc(100% - 32px); /* Full width with padding */
    margin: 0 auto 16px;
    height: 200px;

    .inner {
      padding: 20px;
      width: 100%;
      height: 100%;

      &-title {
        font-size: 18px;
        margin-bottom: 10px;
      }

      &-sub-title {
        font-size: 16px;
        margin-bottom: 8px;
      }

      &-content {
        font-size: 12px;
        margin-bottom: 16px;
      }

      .more-icon {
        width: 18px;
        height: 18px;
      }

      .active-more {
        width: 80px;
        height: 35px;

        span {
          font-size: 16px;
        }

        img {
          width: 16px;
          height: 16px;
        }
      }
    }
  }
}
</style>
